package logic

import (
	"community/communityrpc/communityrpc"
	"community/communityrpc/internal/svc"
	"community/models/mysqlinit"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReleasePostsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReleasePostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReleasePostsLogic {
	return &ReleasePostsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReleasePostsLogic) ReleasePosts(in *communityrpc.ReleasePostsRequest) (*communityrpc.ReleasePostsResponse, error) {
	// todo: add your logic here and delete this line
	var user mysqlinit.User

	mysqlinit.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在，无法评论")
	}
	post := mysqlinit.Post{

		UserId:  in.UserId,
		Image:   in.Image,
		Content: in.Content,
	}
	mysqlinit.DB.Debug().Create(&post)

	return &communityrpc.ReleasePostsResponse{
		Id: int64(post.ID),
	}, nil
}
